<?php
class bargain_management {
	
	function checkBargain($productId,$userId) {
		$sql="SELECT bargain_id,amount FROM tbl_bargain where product_id='$productId' and user_id='$userId'";
		$rst=mysql_query($sql);
		$num=mysql_num_rows($rst);
		if($num > 0) { $row = mysql_fetch_array($rst); return $row; }
		else { return false; }
	}
	
	function selectBuyerBargain($productId) {
		$sql="SELECT * FROM tbl_bargain where product_id='$productId' and status='offer'";
		$rst=mysql_query($sql);
		$num=mysql_num_rows($rst);		
		if($num > 0) { 			
			while($row = mysql_fetch_array($rst)) {
				$data[]=$row; 
			}
			return $data;
		}
		else { return false; }
	}
	
	function selectBargainByParent($productId,$parentId) {
		$sql="SELECT * FROM tbl_bargain where parent_id='$parentId' and product_id='$productId'";
		$rst=mysql_query($sql);
		$num=mysql_num_rows($rst);	
		if($num > 0) { 			
			$row = mysql_fetch_array($rst);				
			return $row;
		}
		else { return false; }
	}
	
	function selectByBargainId($bargainId) {
		$sql="SELECT * FROM tbl_bargain where bargain_id='$bargainId'";
		$rst=mysql_query($sql);
		$num=mysql_num_rows($rst);		
		if($num > 0) { 			
			while($row = mysql_fetch_array($rst)) {
				$data[]=$row; 
			}
			return $data;
		}
		else { return false; }
	}
	
	function addSellerReply($productId,$userId,$askingAmount,$offerAmount,$comment,$parentBargain) {
		$sql="INSERT INTO tbl_bargain set product_id='$productId', asking_amount='$askingAmount', 
			  user_id='$userId',parent_id='$parentBargain',user_type='1',offer_amount='$offerAmount', 
			  comment='$comment', status='reply'";
		$rst=mysql_query($sql);	
		if($rst) { return true; }
		else { return false; }	
	}
	
	function addBuyerBargain($productId,$userId,$askingAmount,$offerAmount,$comment) {		
		$sql="INSERT INTO tbl_bargain set product_id='$productId', asking_amount='$askingAmount', 
			  user_id='$userId',user_type='3',offer_amount='$offerAmount', comment='$comment', status='offer'";
		$rst=mysql_query($sql);	
		if($rst) { return true; }
		else { return false; }		
	}
	
	function delBargainById($bargainId) {
		$sql="DELETE FROM tbl_bargain where bargain_id='$bargainId'";
		$rst=mysql_query($sql);
		if($rst) { return true; }
		else { return false; }
	}		
}	


?>